//
// Created by 77873 on 2020/7/15 0015.
//

#include "JavaListener.h"


JavaListener::JavaListener(JavaVM *vm, _JNIEnv *env, jobject obj) {
    jvm = vm;
    jniEnv=env;
    jobj = obj;


    jclass clz=env->GetObjectClass(jobj);

    if (!clz){
        return ;
    }

    jmid=env->GetMethodID(clz,"onError","(ILjava/lang/String;)V");





}

JavaListener::~JavaListener() {

}

void JavaListener::onError(int threadType, int code, const char *msg) {
    if (threadType==0){

        jstring  jmsg=jniEnv->NewStringUTF(msg);

        jniEnv->CallVoidMethod(jobj,jmid,code,msg);

        jniEnv->DeleteLocalRef(jmsg);


    } else if (threadType==1){

        JNIEnv *env;

        jvm->AttachCurrentThread(&env,0);

        jstring  jmsg=env->NewStringUTF(msg);

        env->CallVoidMethod(jobj,jmid,code,msg);

        env->DeleteLocalRef(jmsg);

        jvm->DetachCurrentThread();
    }
}
